<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\index\controller;

use com\CityUtils;
use com\GeoCityUtils;
use mip\ApiBase;
use mip\AuthApiBase;
use mip\Init;
use think\Controller;
use think\Log;
use think\Request;
use think\Response;
use think\Session;
use mip\Mip;
use com\Network;
use app\common\controller\Common;
use WeMini;
use WeChat\Script;


class ApiAnonyMipPix extends Init
{
    public function do_pix()
    {
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Credentials: true");

        $tt = input('tt');

        $arrtt = json_decode($tt, true);
        $url = $_SERVER['HTTP_REFERER'];
        $arrtt['url'] = $url;

        $wxUserinfo = session('wx_userinfo');

        $loadId = $arrtt['loadId'];

        if( session('screen_width') == null){
            session('screen_width',$arrtt['screenSize']['width']);
        }
        if(Network::isMobile()){
            $this->isMobile = true;
        }else{
            $this->isMobile = false;
        }
        $clientId=$arrtt['clientId'];
        $visitSession = db('visit_session')->where('client_id', $clientId)->find();
        if ($visitSession === null) {
            $visitSession = [];
            $visitSession['is_mobile'] = $this->isMobile;
            $visitSession['is_weixin'] = $this->isWeixin;
            $visitSession['client_id'] = $arrtt['clientId'] != null ? $arrtt['clientId']:session_id() ;
            $visitSession['first_visit_time'] = date('Y-m-d H:i:s', $arrtt['openTime']/1000);
            $visitSession['pv_count'] = 1;
            $visitSession['os_info'] = $arrtt['os'];
            if($arrtt['os'] !=null){
                if(strpos($arrtt['os'],'Win') !== false){
                    $visitSession['os_name'] = 'Win';
                }else if(strpos($arrtt['os'],'Mac') !== false){
                    $visitSession['os_name'] = 'Mac';
                }else if(strpos($arrtt['os'],'Android') !== false){
                    $visitSession['os_name'] = 'Android';
                }else if(strpos($arrtt['os'],'iOS') !== false){
                    $visitSession['os_name'] = 'iOS';
                }else if(strpos($arrtt['os'],'Linux') !== false){
                    $visitSession['os_name'] = 'Linux';
                }else if(strpos($arrtt['os'],'Unix') !== false){
                    $visitSession['os_name'] = 'Unix';
                }else {
                    $visitSession['os_name'] = 'other';
                }
            }
            $visitSession['browser_info'] = $arrtt['browser'];
            if($arrtt['browser'] !=null){
                if(strpos($arrtt['browser'],'IE') !== false){
                    $visitSession['browser_name'] = 'IE';
                }else if(strpos($arrtt['browser'],'firefox')  !== false ){
                    $visitSession['browser_name'] = 'firefox';
                }else if(strpos($arrtt['browser'],'chrome')  !== false ){
                    $visitSession['browser_name'] = 'chrome';
                }else if(strpos($arrtt['browser'],'safari')  !== false){
                    $visitSession['browser_name'] = 'safari';
                }else {
                    $visitSession['browser_name'] = 'other';
                }
            }

            $visitSession['screen_width'] = $arrtt['screenSize']['width'];
            $visitSession['screen_height'] = $arrtt['screenSize']['height'];

            // 获取客户端ip
            $visitSession['client_ip'] = request()->ip();
            // 根据Ip获取 geo_city_pinyin
            $geoInfo =GeoCityUtils::getGeoInfo($visitSession['client_ip']);
            if($geoInfo != null){
                $visitSession['geo_city_pinyin'] = $geoInfo['city'];
                $visitSession['geo_lat'] = $geoInfo['latitude'];
                $visitSession['geo_lng'] = $geoInfo['longitude'];

                // 根据city_pinyin 获取city信息
                $cityInfo = CityUtils::getCityByPinyin( $geoInfo['city'] );
                if($cityInfo!=null){
                    $visitSession['geo_city_name'] = $cityInfo['city'];
                    $visitSession['geo_city_code'] = $cityInfo['code'];

                    $visitSession['geo_province_code'] = $cityInfo['provincecode'];
                }
            }

            if ($wxUserinfo != null && is_array($wxUserinfo)) {
                $visitSession['wx_nickname'] = $wxUserinfo['nickname'];
                $visitSession['wx_sex'] = $wxUserinfo['sex'];
                $visitSession['wx_headimg'] = $wxUserinfo['headimgurl'];
                $visitSession['wx_openid'] = $wxUserinfo['openid'];
                $visitSession['wx_country'] = $wxUserinfo['country'];
                $visitSession['wx_province'] = $wxUserinfo['province'];
                $visitSession['wx_city'] = $wxUserinfo['city'];
            }

            db('visit_session')->insert($visitSession);

        } else {
            $visitSession['last_visit_time'] = $arrtt['closeTime'] == null ? null : date('Y-m-d H:i:s', $arrtt['closeTime']/1000);
            $visitSession['pv_count'] = $visitSession['pv_count']+1;
            db('visit_session')->where('id',$visitSession['id'])->update($visitSession);
        }

        $visitLog = db('visit_log')->where('request_id', $loadId)->find();
        if ($visitLog === null) {
            $visitLog = [];
            $visitLog['is_mobile'] = $this->isMobile;
            $visitLog['is_weixin'] = $this->isWeixin;
            $visitLog['client_id'] = $arrtt['clientId'] != null ? $arrtt['clientId']:session_id() ;
            $visitLog['request_id'] = $arrtt['loadId'];
            $visitLog['open_time'] = date('Y-m-d H:i:s', $arrtt['openTime']/1000);
            $visitLog['page_referrer'] = $arrtt['referrer'];
            $visitLog['page_url'] = $arrtt['url'];
            $visitLog['page_title'] = $arrtt['title'];
            $visitLog['close_time'] = $arrtt['closeTime'] == null ? null : date('Y-m-d H:i:s', $arrtt['closeTime']/1000);
            $visitLog['visit_second'] = $arrtt['closeTime'] == null ? null : ($arrtt['closeTime'] - $arrtt['openTime'])/1000;
            $visitLog['os_info'] = $arrtt['os'];
            if($arrtt['os'] !=null){
                if(strpos($arrtt['os'],'Win') !== false){
                    $visitLog['os_name'] = 'Win';
                }else if(strpos($arrtt['os'],'Mac') !== false){
                    $visitLog['os_name'] = 'Mac';
                }else if(strpos($arrtt['os'],'Android') !== false){
                    $visitLog['os_name'] = 'Android';
                }else if(strpos($arrtt['os'],'iOS') !== false){
                    $visitLog['os_name'] = 'iOS';
                }else if(strpos($arrtt['os'],'Linux') !== false){
                    $visitLog['os_name'] = 'Linux';
                }else if(strpos($arrtt['os'],'Unix') !== false){
                    $visitLog['os_name'] = 'Unix';
                }else {
                    $visitLog['os_name'] = 'other';
                }
            }
            $visitLog['browser_info'] = $arrtt['browser'];
            if($arrtt['browser'] !=null){
                if(strpos($arrtt['browser'],'IE') !== false){
                    $visitLog['browser_name'] = 'IE';
                }else if(strpos($arrtt['browser'],'firefox')  !== false ){
                    $visitLog['browser_name'] = 'firefox';
                }else if(strpos($arrtt['browser'],'chrome')  !== false ){
                    $visitLog['browser_name'] = 'chrome';
                }else if(strpos($arrtt['browser'],'safari')  !== false){
                    $visitLog['browser_name'] = 'safari';
                }else {
                    $visitLog['browser_name'] = 'other';
                }
            }

            $visitLog['screen_width'] = $arrtt['screenSize']['width'];
            $visitLog['screen_height'] = $arrtt['screenSize']['height'];
            $visitLog['document_width'] = $arrtt['documentSize']['width'];
            $visitLog['document_height'] = $arrtt['documentSize']['height'];
            $visitLog['scroll_top'] = $arrtt['scrollTop'];
            $visitLog['scroll_height'] = $arrtt['scrollHeight'];
            $visitLog['client_height'] = $arrtt['clientHeight'];

            // 获取客户端ip
            $visitLog['client_ip'] = request()->ip();
            // 根据Ip获取 geo_city_pinyin
//            $geoInfo =GeoCityUtils::getGeoInfo($visitLog['client_ip']);
//            if($geoInfo != null){
//                $visitLog['geo_city_pinyin'] = $geoInfo['city'];
//                $visitLog['geo_lat'] = $geoInfo['latitude'];
//                $visitLog['geo_lng'] = $geoInfo['longitude'];
//
//                // 根据city_pinyin 获取city信息
//                $cityInfo = CityUtils::getCityByPinyin( $geoInfo['city'] );
//                if($cityInfo!=null){
//                    $visitLog['geo_city_name'] = $cityInfo['city'];
//                    $visitLog['geo_city_code'] = $cityInfo['code'];
//
//                    $visitLog['geo_province_code'] = $cityInfo['provincecode'];
//                }
//            }

//            if ($wxUserinfo != null && is_array($wxUserinfo)) {
//                $visitLog['wx_nickname'] = $wxUserinfo['nickname'];
//                $visitLog['wx_sex'] = $wxUserinfo['sex'];
//                $visitLog['wx_headimg'] = $wxUserinfo['headimgurl'];
//                $visitLog['wx_openid'] = $wxUserinfo['openid'];
//                $visitLog['wx_country'] = $wxUserinfo['country'];
//                $visitLog['wx_province'] = $wxUserinfo['province'];
//                $visitLog['wx_city'] = $wxUserinfo['city'];
//            }

            db('visit_log')->insert($visitLog);

        } else {
            if (!empty($arrtt['closeTime']) && $arrtt['closeTime'] > 0) {
                $visitLog['close_time'] = date('Y-m-d H:i:s', $arrtt['closeTime']/1000);
            }
            if ($arrtt['scrollTop'] > $visitLog['scroll_top']) {
                $visitLog['scroll_top'] = $arrtt['scrollTop'];
            }
            if ($arrtt['scrollHeight'] > $visitLog['scroll_height']) {
                $visitLog['scroll_height'] = $arrtt['scrollHeight'];
            }
            if ($arrtt['clientHeight'] > $visitLog['client_height']) {
                $visitLog['client_height'] = $arrtt['clientHeight'];
            }
            db('visit_log')->where('id',$visitLog['id'])->update($visitLog);
        }

        return '0';
    }


}
